
import { _decorator, Component, Node, TiledMap, TiledLayer, TiledTile, TiledObjectGroup, Sprite, SpriteFrame
    , find, log ,url, resources,Color,Texture2D, UITransform, EventHandler, Button} from 'cc';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = Main
 * DateTime = Sat Oct 16 2021 13:54:52 GMT+0800 (中国标准时间)
 * Author = yly1234560
 * FileBasename = main.ts
 * FileBasenameNoExtension = main
 * URL = db://assets/scripts/main.ts
 * ManualUrl = https://docs.cocos.com/creator/3.3/manual/zh/
 *
 */
 
@ccclass('Main')
export class Main extends Component {
    // [1]
    // dummy = '';

    // [2]
    // @property
    // serializableDummy = 0;
    @property({type:TiledMap})
    private tiledMap;
    @property({type:Sprite})
    private ylyTestScriptUrl;
    @property({type:Sprite})
    private ylytest1;

    private footprint_node:Node;

    private newNode:Node[] = [null];

    start () {
        // [3]
        this.test();
    }

    update (deltaTime: number) {
        // [4]
        if(this.ylyTestScriptUrl.node.position.x>1000){
            this.ylyTestScriptUrl.node.position.x = 0;
            this.ylyTestScriptUrl.node.position.y = 0;
        }

        this.ylyTestScriptUrl.node.setPosition(this.ylyTestScriptUrl.node.position.x+1,this.ylyTestScriptUrl.node.position.y+1);


        if(this.newNode!=null && this.newNode.length>0 && this.newNode[0] != null){

            let node = this.newNode[0];
            // if(node.position.x>1000){
            //     node.setPosition(0,0);
            // }

            // log("*** i'm in ****");
            node.setPosition(this.ylyTestScriptUrl.node.position.x-100,this.ylyTestScriptUrl.node.position.y);
            // log(node.position.x+"********"+node.position.y);
        }

    }

    test () {
        let pieces = this.node.getChildByName("pieces");
        // let node=find("Canvas/TiledMap/pieces");
        // console.log(node);
        // console.log(node);

        console.log("pieces.getComponent(TiledObjectGroup):");
        console.log(pieces.getComponent(TiledObjectGroup));
        console.log(pieces.getComponent(TiledObjectGroup).getProperty("power"));
        console.log(pieces.getComponent(TiledObjectGroup).getObjects());

        let tog = pieces.getComponent(TiledObjectGroup);
        let p = tog.getObjects();
        for(let i = 0;i<p.length;i++){
            if(p[i].name == "r_lion"){
                console.log("p[i].name");
                // console.log(p[i].node.getPosition());



            }
        }


        let nodes = pieces.children;
        console.log(nodes.length);

        for(let i = 0;i<nodes.length;i++){
            if(nodes[i].name == "r_lion"){
                console.log("nodes[i].getComponent(TiledObjectGroup):");
                console.log(nodes[i].getComponent(TiledObjectGroup));
            }
        }


        // let scripts = this.node.getChildByName("pieces").getComponent(Sprite);

        // console.log(scripts);

        // for(let i = 0;i<scripts.length;i++){
        //     console.log(pieces[i].name);
        // }

        console.log(pieces);
        console.log(pieces);
        // pieces.getComponent("")
        //
        // for(let i = 0;i<pieces.length;i++){
        //     console.log(pieces[i].name);
        // }


        let map1 = this.node.getChildByName("map1").getComponent(TiledLayer);
        console.log(map1.getLayerName());
        console.log(map1.getTiledTileAt(3, 5));
        // console.log("********"+map1.getTiledTileAt(4, 4).name);



        let layerSize = map1.getLayerSize();
        console.log("********"+layerSize.width+"***"+layerSize.height);
        let width = layerSize.width;
        let height = layerSize.height;
        let gids=[];
        for(let i=0;i<height;i++){
            for(let j=0;j<width;j++){
                gids.push(map1.getTileGIDAt(j,i));
                // console.log("********"+map1.getTileGIDAt(j,i));
            }
        }
        console.log(gids);

    }


    selected(event, customEventData){
        let node = event.target;
        let button = node.getComponent(Sprite);
        console.log("node=", node.name, " event=", event.type, " data=", customEventData);
        node.setPosition(-418,518,0);
        console.log(node.getPosition());

        // node.setPosition(1024,768);





        // 创建一个新节点
        const node_new = new Node();
        // 然后把该节点的父节点设置为任意的 layer 节点
        node_new.parent = this.node.getChildByName("map1").getComponent(TiledLayer).node;
        // 最后添加 TiledTile 组件到该节点上，并返回 TiledTile 对象，就可以对 TiledTile 对象进行一系列操作
        const tiledTile = node_new.addComponent(TiledTile);
        tiledTile.name = "yly";
        tiledTile.x = 5;
        tiledTile.y = 6;
        // tiledTile.
        console.log(tiledTile.x);
        console.log(tiledTile.name);
        node_new.setPosition(-418,518,0);


        const sprite = tiledTile.addComponent(Sprite);



    }

    selected_(event, customEventData){
        let node = event.target;
        let button = node.getComponent(Sprite);
        console.log("node=", node.name, " event=", event.type, " data=", customEventData);
        // node.position.x = this.ylyTestScriptUrl.node.position.x;
        // node.position.y = this.ylyTestScriptUrl.node.position.y;
        // node.setPosition(0,0,0);
        // console.log(node.getPosition());


        let x = this.ylyTestScriptUrl.node.position.x;
        let y = this.ylyTestScriptUrl.node.position.y;

        log("click:**x**y*:"+node.position.x+"****"+node.position.y);
        node.setPosition(x,y);





        let img = this.node.getChildByName("pieces").getChildByName("img2");
        log("img:**x**y*:"+img.position.x+"****"+img.position.y);
        img.setPosition(x,y);

        log("this_sprit**x**y*:"+x+"****"+y);

        // let r_lion = img.getComponent(Sprite);
        // log(r_lion);
        // r_lion.spriteFrame = this.ylyTestScriptUrl.spriteFrame;


        let footprint_node = new Node();
        footprint_node.name = "footprint_ceshi";
        // footprint_node.layer = 1;
        footprint_node.setPosition(x+500,266);
        footprint_node.parent = this.node;
        // footprint_node.parent = this.node.parent;

        // 爪印
        let footprint = footprint_node.addComponent(Sprite);
        // footprint.spriteFrame = this.ylyTestScriptUrl.spriteFrame;


        // 加载多个资源
        // resources.loadDir("pieces", SpriteFrame, (err, assets) => {
        //     console.log(assets);
        //
        //
        // });


        // 加载一个资源
        // 调试测试 过程中遇到的问题  一直无法加载资源，再 creator中 右键重新导入资源后 生效
        // 官方文档 https://docs.cocos.com/creator/3.3/manual/zh/asset/dynamic-load-resources.html#%E5%8A%A0%E8%BD%BD-spriteframe-%E6%88%96-texture2d
        resources.load("pieces/r_lion/spriteFrame", SpriteFrame, (err, spriteFrame) => {
            console.log(spriteFrame);
            footprint.spriteFrame = spriteFrame;

        });

        // for (let i = 2; i <= 17; i++) {
        //     let url = "/pieces/"+i+"/spriteFrame";
        //     resources.load(url, SpriteFrame, (err, spriteFrame) => {
        //         console.log("spriteFrame****"+i+":");
        //         console.log(spriteFrame);
        //         footprint.spriteFrame = spriteFrame;
        //
        //     });
        // }



        console.log(footprint_node);
        console.log(footprint);
        // footprint_node.setPosition(x+500,266);



        log("node.parent.name:"+node.parent.name);
        log("this.node.name:"+this.node.name);
        log("this.node.position.z:"+this.node.position.z);
        log("footprint_node:"+footprint_node.parent.name);
        log("footprint_node:"+footprint_node.getPosition());
        log("footprint_node.position.x:"+footprint_node.position.x);
        log("footprint_node.position.y:"+footprint_node.position.y);

        let footprint_ui = footprint_node.getComponent(UITransform);
        log("footprint_node.footprint_ui:"+footprint_ui.anchorPoint.toString());
        log("footprint_node.footprint_ui:"+footprint_ui.contentSize.width);
        footprint_ui.setContentSize(500,500);
        // footprint_ui.setAnchorPoint(0,0);
        // footprint_ui.setContentSize(150,150);
        footprint_node.setPosition(100,100,999);
        footprint_node.setPosition(100,100,999);
        log("footprint_node.position.x:"+footprint_node.position.x);
        log("footprint_node.position.y:"+footprint_node.position.y);
        log("footprint.nodeName:"+footprint.node.name);
        log("footprint.node..position.x:"+footprint.node.position.x);
        log("footprint.node.position.y:"+footprint.node.position.y);
        log("footprint.node.position.z:"+footprint.node.position.z);
        // footprint_node.set



        footprint_node.setPosition(68,68,999);
        log(this.ylyTestScriptUrl.node);
        log(footprint.node);
        log("this.ylyTestScriptUrl.position:"+this.ylyTestScriptUrl.node);
        log("footprint.node.getPosition():"+footprint.node);
        log("this.ylyTestScriptUrl.position:"+this.ylyTestScriptUrl.node.getPosition());
        log("footprint.node.getPosition():"+footprint.node.getPosition());


        log("node.layer:"+node.layer);
        log("this.ylyTestScriptUrl.node.layer:"+this.ylyTestScriptUrl.node.layer);
        log("footprint.node.layer:"+footprint.node.layer);


        // 新建节点一直未显示 添加次layer后显示
        footprint_node.layer = this.ylyTestScriptUrl.node.layer;

        // footprint.node = this.ylyTestScriptUrl.node;
        // footprint_node.setPosition(x-100,y,999);




        const clickEventHandler = new EventHandler();
        clickEventHandler.target = this.node; // 这个 node 节点是你的事件处理代码组件所属的节点
        // clickEventHandler.component = 'Main';// 这个是脚本类名
        // clickEventHandler.handler = 'callback';
        clickEventHandler.component = 'BtnExample';// 这个是脚本类名
        clickEventHandler.handler = 'callback';
        clickEventHandler.customEventData = 'foobar';

        const button_ = footprint_node.addComponent(Button);
        button_.clickEvents.push(clickEventHandler);


        log(footprint_node);
        log(node);


        this.newNode[0] = footprint_node;

        // let fp = this.node.getChildByName("footprint_ceshi");
        // fp.setPosition(x+500,0);




        // let node_ = new Node('nodeName');
        // let texture = new Texture2D();
        // let spriteFrame = new SpriteFrame();
        // texture.initWithData(new Uint8Array([0, 0, 0]), cc.Texture2D.PixelFormat.RGB888, 1, 1, cc.winSize);
        // spriteFrame.setTexture(texture);
        // spriteFrame.setRect(cc.rect(0, 0, cc.winSize.width * 20, cc.winSize.width * 20));
        // node.addComponent(cc.Sprite).spriteFrame = spriteFrame;





        if(true)return;


        const self = this;
        const url = "pieces/2";


        resources.load(url, SpriteFrame, (err: any, spriteFrame) => {
            const sprite = self.node.addComponent(Sprite);
            sprite.name="ylytestsprite_new";
            sprite.spriteFrame = spriteFrame;
        });


        // let component = this.node.getChildByName("ylytestsprite_new").getComponent(Sprite);
        // log(component.node.name);



        // if(true)return;



        // 获取精灵 资源文件中 的 url
        log("ylyTestScriptUrl:"+this.ylyTestScriptUrl.nativeUrl);
        log("ylyTestScriptUrl:"+this.ylyTestScriptUrl.name);
        log("ylyTestScriptUrl:"+this.ylyTestScriptUrl.spriteFrame);



        let tiledLayer = this.node.getChildByName("map2").getComponent(TiledLayer);
        tiledLayer.getLayerName();
        console.log("****tiledLayer.getLayerName()***:"+tiledLayer.getLayerName());
        // tiledTileAt.;


        console.log("****tiledLayer.getMapTileSize()***:"+tiledLayer.getMapTileSize());


        console.log(tiledLayer.getProperties());

        // let map1_tl = this.node.getChildByName("map1").getComponent(TiledLayer);

        // let tiledTileAt = tiledLayer.getTiledTileAt(2,2,true);
        // let spr = tiledTileAt.node.addComponent(Sprite);
        // // let raw_ = url.raw("f");
        // spr.spriteFrame= this.ylyTestScriptUrl.spriteFrame;
        // spr.type = Sprite.Type.SIMPLE;
        // spr = this.ylyTestScriptUrl;
        // // tiledTileAt.
        // console.log(spr);
        // console.log(tiledTileAt);
        // console.log(tiledLayer.getTiledTileAt(2,2));
        // console.log(tiledTileAt.enabled);

        // for (let i = 0; i < tiles.length; i++) {
        //
        // }

        // if(true)return;

        // spr.color = new Color(0,1,2,3);

        // let map1 = this.node.getChildByName("map1").getComponent(TiledLayer);
        // console.log(map1.getTiledTileAt(3, 4));
        // console.log(map1.getTiledTileAt(0, 0));
        // console.log(map1.getTiledTileAt(1, 1));
        // console.log(map1.getTiledTileAt(3, 4));

        // map1.removeUserNode();

        for (let i = 0; i < 7; i++) {
            for (let j = 0; j < 9; j++) {
                // console.log("i****j***:"+i+"*****"+j+"*******"+map1.getTiledTileAt(i, j));
                // console.log("i****j***:"+i+"*****"+j+"*******"+map1.getPositionAt(i, j));
                // console.log("i****j***:"+i+"*****"+j+"*******GID:"+map1.getTileGIDAt(i, j));


                let tiledTileAt = tiledLayer.getTiledTileAt(i,j,true);
                tiledTileAt.node.setPosition(100,100);
                tiledTileAt.node.setRotation(0,1,2,3);
                tiledTileAt.node.setScale(50,60);
                let spr = tiledTileAt.node.addComponent(Sprite);
                // let raw_ = url.raw("f");
                spr.spriteFrame= this.ylyTestScriptUrl.spriteFrame;
                spr.type = Sprite.Type.SIMPLE;
                spr.color = new Color(0,1,2,3);
                spr = this.ylyTestScriptUrl;


                // spr.node = this.node;

            }
        }

        let pieces = this.tiledMap.getObjectGroup('pieces');
        console.log(pieces);
        console.log("pieces***:"+pieces);
        console.log("pieces.getObject('img2')***:"+pieces.getObject('r_lion'));

        let startPoint = pieces.getObject('r_lion');
        let endPoint = pieces.getObject('r_tiger');
        let r_dog = pieces.getObject('r_dog');

        // let startPos = cc.position(startPoint.offset.x, startPoint.offset.y);
        // let endPos = cc.p(endPoint.offset.x, endPoint.offset.y);

        console.log("startPoint.name***:"+startPoint.name);
        console.log("startPoint.offset.x****startPoint.offset.y***:"+startPoint.offset.x+"****"+startPoint.offset.y);
        console.log("endPoint.offset.x****endPoint.offset.y***:"+endPoint.offset.x+"****"+endPoint.offset.y);
        console.log("r_dog.offset.x****r_dog.offset.y***:"+r_dog.offset.x+"****"+r_dog.offset.y);

        // startPoint.setPosition(146,444);
        // startPoint.offset.y = 444;


        for (let i = 0; i < 7; i++) {
            for (let j = 0; j < 9; j++) {

            }
        }





        // let tiledTile = this.node.getChildByName("pieces").getComponent(TiledLayer);
        // for (let i = 0; i < 7; i++) {
        //     for (let j = 0; j < 7; j++) {
        //         console.log("i****j***:"+tiledTile.getTiledTileAt(i, j));
        //     }
        // }
    }

    // 添加精灵的点击事件
    callback (event: Event, customEventData: string) {

        console.log("I'm in"); // foobar


        // 这里 event 是一个 Touch Event 对象，你可以通过 event.target 取到事件的发送节点
        const node = event.target as unknown as Node;
        // const button = node.getComponent(Button);
        console.log(customEventData); // foobar
        console.log(node.name); // foobar
    }

}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.3/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.3/manual/zh/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.3/manual/zh/scripting/life-cycle-callbacks.html
 */
